package com.fudan.biz.visualize.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fudan.biz.visualize.service.AggregationService;
import com.fudan.es.aggs.entity.ChartResponseEntity;
import com.fudan.framework.entity.ResponseMsg;

@RestController
@RequestMapping("/aggs")
public class AggregationController {

	@Autowired
	private AggregationService aggregationService;


	String json = "{\r\n" +
			"	\"dsId\":\"24\",\r\n" +
			"	\r\n" +
			"	\"metric\": [\r\n" +
			"		{\r\n" +
			"			\"type\": \"terms\",\r\n" +
			"			\"field\": \"739\",\r\n" +
			"			\"metric\": [\r\n" +
			"				{\r\n" +
			"					\"type\": \"avg\",\r\n" +
			"					\"field\": \"1183\"\r\n" +
			"				},\r\n" +
			"				{\r\n" +
			"					\"type\": \"terms\",\r\n" +
			"					\"field\": \"1196\"\r\n" +
			"				}\r\n" +
			"			]\r\n" +
			"		},\r\n" +
			"		{\r\n" +
			"			\"type\": \"terms\",\r\n" +
			"			\"field\": \"1192\",\r\n" +
			"\"grain\":\"__root\""+
			"		}\r\n" +
			"	]\r\n" +
			"}";

	@RequestMapping("/test")
	@ResponseBody
	public ResponseMsg test(HttpServletRequest request) {
		String req = request.getParameter("req");
		try {
				ChartResponseEntity chartResponseEntity = aggregationService.aggsResponse(req);
				System.out.println(chartResponseEntity.getResponseData());
				return ResponseMsg.build().putData(chartResponseEntity.getResponseData());
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMsg.build(500, "失败");
		}
	}
}
